package ph.com.gs3.formalistics.model.communicators;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import android.annotation.SuppressLint;

public class FormCommunicatorModule extends HttpCommunicatorModule {

	public static final String TAG = FormCommunicatorModule.class.getSimpleName();

	public FormCommunicatorModule(HttpCommunicator communicator) {
		super(communicator);
	}

	public FormCommunicatorModule(HttpCommunicator communicator, String server) {
		super(communicator, server);
	}

	@SuppressLint("SimpleDateFormat")
	public APIResponse updateAllForms(String fromDate) throws APIInvalidResponseException,
	        CommunicatorException {

		commonValidation();

		String url = server + "/API/form-list";

		// prepare the parameter for filtering by date updated

		Map<String, String> requestParams = null;

		if (fromDate != null && !fromDate.isEmpty()) {
			JSONObject dateUpdatedFilter = new JSONObject();
			JSONObject dateUpdatedConditions = new JSONObject();
			try {
				dateUpdatedConditions.put("condition", ">=");
				dateUpdatedConditions.put("compared_to_date", fromDate);

				dateUpdatedFilter.put("date_updated_comparison", dateUpdatedConditions);

				requestParams = new HashMap<String, String>();

				requestParams.put("search_parameters", dateUpdatedFilter.toString());

				FLLogger.d(TAG, "Added search parameter, date updated should be >= " + fromDate);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		return getResponse(url, requestParams);

	}

}
